package com.yidu.mt.service.impl;

import com.yidu.mt.controller.vo.MtUserVo;
import com.yidu.mt.dao.MtUserMapper;
import com.yidu.mt.domain.MtUser;
import com.yidu.mt.service.LogAndRegService;
import javafx.animation.KeyFrame;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;

/**
 * <p>@Description: No Description</p>
 * <p>@author zhang hao chao</p>
 * <p>@Date: 2021/1/24</p>
 * <p>@Time: 17:00</p>
 */
@Service
public class LogAndRegServiceImpl implements LogAndRegService {

    @Resource
    MtUserMapper mtUserMapper;


    @Override
    public int deleteByPrimaryKey(Integer userId) {
        return mtUserMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int insert(MtUserVo vo) {
        MtUser po=new MtUser();
        BeanUtils.copyProperties(vo,po);

        /**
         * 获取当前时间
         */
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        String da=dateFormat.format(calendar.getTime());


        if (!StringUtils.isEmpty(vo.getUserBirthday_())){
            po.setUserBirthday(Date.valueOf(vo.getUserBirthday_()));

            /**
             * 获取年龄 放进po
             */
            java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
            java.util.Date beginDate= null;
            java.util.Date endDate=null;
            try {
                beginDate = format.parse(vo.getUserBirthday_());
                endDate= format.parse(da);
                long day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
                int l= (int) (day/365);
                po.setUserAge(l);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (!StringUtils.isEmpty(da)){
            po.setUserDate(Date.valueOf(da));//获取注册时间

            Random random=new Random();
            int max1=random.nextInt(100  );
            int max2=random.nextInt(100);
            int max3=random.nextInt(100);
            po.setUserIp(po.getUserNickname()+max1+max2+max3);//随机生成IP放进po
        }



        return mtUserMapper.insert(po);
    }

    @Override
    public int insertSelective(MtUser record) {
        return mtUserMapper.insertSelective(record);
    }

    @Override
    public MtUser selectByPrimaryKey(Integer userId) {
        return mtUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(MtUser record) {
        return mtUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MtUser record) {
        return mtUserMapper.updateByPrimaryKey(record);
    }

    @Override
    public int selectCountNameAndPassword(String userName, String userPassword) {
        return mtUserMapper.selectCountNameAndPassword(userName,userPassword);
    }

}
